package com.zyh.jsp.controller;

import com.zyh.jsp.util.VerifyCode;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;

@Controller
public class MainController {

    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index() {
        return "index";
    }

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String login() {
        return "jsp/login/login";
    }

    @RequestMapping(value = "/register", method = RequestMethod.GET)
    public String register(HttpServletRequest request) {
        String step = request.getParameter("step");
        String path;
        if (step.equals("one")) {
            path = "jsp/register/register_one";
        } else if (step.equals("two")) {
            path = "jsp/register/register_two";
        } else {
            path = "jsp/register/register_three";
        }
        return path;
    }


    @RequestMapping(value = "/password", method = RequestMethod.GET)
    public String password(HttpServletRequest request) {
        String step = request.getParameter("step");
        String path;
        if (step.equals("one")) {
            path = "jsp/password/password_one";
        } else if (step.equals("two")) {
            path = "jsp/password/password_two";
        } else {
            path = "jsp/password/password_three";
        }
        return path;
    }

    @RequestMapping(value = "/getVerifyCode", method = RequestMethod.GET)
    public void getVerifyCode(HttpServletResponse response, HttpServletRequest request) {
        try {
            int width = 93;
            int height = 40;
            BufferedImage verifyImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            String randomText = VerifyCode.drawRandomText(width, height, verifyImg);
            request.getSession().setAttribute("verifyCode", randomText);
            response.setContentType("image/png");//必须设置响应内容类型为图片，否则前台不识别
            OutputStream os = response.getOutputStream(); //获取文件输出流
            ImageIO.write(verifyImg,"png",os);//输出图片流
            os.flush();
            os.close();//关闭流
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping(value = "/getCodePhone",method = RequestMethod.GET)
    @ResponseBody
    public String getCodePhone(HttpServletRequest request){
        String phone = request.getParameter("phone");
        return "success";
    }
}
